#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
号码匹配模块
负责匹配用户号码与开奖号码，并计算中奖结果
"""

from lottery_rules import get_lottery_rule

class NumberMatcher:
    """
    号码匹配器
    """
    def __init__(self):
        pass
    
    def match_tickets(self, user_tickets, winning_numbers):
        """
        匹配用户号码与开奖号码
        user_tickets: 用户号码列表
        winning_numbers: 开奖号码列表
        返回匹配结果
        """
        results = []
        
        for ticket in user_tickets:
            ticket_type = ticket["type"]
            ticket_numbers = ticket["numbers"]
            
            # 查找对应的开奖号码
            matching_winning = None
            for winning in winning_numbers:
                if winning["type"] == ticket_type and winning["date"] == ticket["date"]:
                    matching_winning = winning
                    break
            
            if matching_winning:
                # 执行匹配
                result = self.match_single_ticket(
                    ticket_type, 
                    ticket_numbers, 
                    matching_winning["numbers"]
                )
                result["ticket_date"] = ticket["date"]
                result["ticket_type"] = ticket_type
                results.append(result)
        
        return results
    
    def match_single_ticket(self, lottery_type, user_numbers, winning_numbers):
        """
        匹配单张彩票
        lottery_type: 彩票类型
        user_numbers: 用户号码
        winning_numbers: 开奖号码
        """
        rule = get_lottery_rule(lottery_type)
        if not rule:
            return {
                "error": f"不支持的彩票类型: {lottery_type}"
            }
        
        try:
            # 计算中奖等级
            level = rule.calculate_winning_level(user_numbers, winning_numbers)
            prize_info = rule.get_prize_info(level)
            
            return {
                "user_numbers": user_numbers,
                "winning_numbers": winning_numbers,
                "winning_level": level,
                "prize_info": prize_info,
                "is_winning": level > 0
            }
        except Exception as e:
            return {
                "error": f"匹配过程中出错: {str(e)}"
            }
    
    def highlight_matches(self, user_numbers, winning_numbers, lottery_type):
        """
        高亮显示匹配的号码
        """
        if lottery_type == "double_color_ball":
            user_red, user_blue = user_numbers
            winning_red, winning_blue = winning_numbers
            
            # 找出匹配的红球
            matched_red = list(set(user_red) & set(winning_red))
            
            # 检查蓝球是否匹配
            blue_matched = (user_blue == winning_blue)
            
            return {
                "matched_red": matched_red,
                "blue_matched": blue_matched
            }
        elif lottery_type == "super_lotto":
            user_front, user_back = user_numbers
            winning_front, winning_back = winning_numbers
            
            # 找出匹配的前区和后区号码
            matched_front = list(set(user_front) & set(winning_front))
            matched_back = list(set(user_back) & set(winning_back))
            
            return {
                "matched_front": matched_front,
                "matched_back": matched_back
            }
        
        return {}

# 使用示例和测试代码
if __name__ == "__main__":
    matcher = NumberMatcher()
    
    # 测试双色球匹配
    user_numbers = [[1, 2, 3, 4, 5, 6], 7]
    winning_numbers = [[1, 2, 3, 4, 5, 6], 7]
    
    result = matcher.match_single_ticket("double_color_ball", user_numbers, winning_numbers)
    print("双色球匹配结果:", result)
    
    # 测试大乐透匹配
    user_numbers = [[1, 2, 3, 4, 5], [1, 2]]
    winning_numbers = [[1, 2, 3, 4, 5], [1, 2]]
    
    result = matcher.match_single_ticket("super_lotto", user_numbers, winning_numbers)
    print("大乐透匹配结果:", result)
    
    # 测试高亮显示
    highlight = matcher.highlight_matches(user_numbers, winning_numbers, "super_lotto")
    print("高亮匹配结果:", highlight)